package com.itheima.travel;

import com.itheima.travel.config.MybatisConfig;
import com.itheima.travel.config.RedissonConfig;
import com.itheima.travel.config.SpringConfig;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class ProjectConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    /*
    * 监听ServletContext全文域对象初始化时加载配置
    * */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class, MybatisConfig.class};
    }

    /*
    * 前端控制器加载配置
    * */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{PlatformSpringMvcConfig.class};
    }

    /*
    * 前端控制器拦截路径
    * */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /*
    * 过滤器字符编码处理
    * */

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);

        FilterRegistration.Dynamic dynamic = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
        dynamic.setInitParameter("encoding", "UTF-8");
        //定义过滤的dispatcher类型，是否匹配之前，过滤路径
        dynamic.addMappingForUrlPatterns(null,true,"/*");
    }
}
